package checkDetail;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import general.BaseBean;

public class WorkflowStatusFieldLengthCheck extends BaseBean implements CheckBase  {

    @Override
    public JSONObject checkData(HashMap dataMap) {
        setRecordSet();
        (new BaseBean()).writeLog("WorkflowStatusFieldLenghCheck checkData start");
        String dbType = recordSet.getDBType();
        String checkSql = "";
        //判断数据库类型
        if("sqlserver".equals((dbType))) {
            checkSql = "select length from SysColumns where id=Object_Id('workflow_requestbase') and name = 'STATUS'";
        }else{
            checkSql = "select DATA_LENGTH as length from user_tab_columns where table_name = UPPER('workflow_requestbase') and COLUMN_NAME = 'STATUS'";
        }
        
        recordSet.executeSql(checkSql);
        List resultList = new ArrayList();
        String result = "";
        while(recordSet.next()){
            int length = recordSet.getInt("length");
            if(length < 500){
                result = resultNeedFix;
                Map resultMap = new HashMap<String, String>();
                resultMap.put("showMessage", "流程表中status字段长度为" + length + "，小于500。");
                resultMap.put("buttonname","修复");
                resultList.add(resultMap);
            }else{
                result = resultOk;
                resultList.add("流程表中status字段长度正常。");
            }
        }
        (new BaseBean()).writeLog("WorkflowStatusFieldLengthCheck checkData end");
        return formatReturnJsonCheck(result, resultList, this);
    }

    @Override
    public JSONObject fixData(HashMap dataMap) {
        setRecordSet();
        (new BaseBean()).writeLog("WorkflowStatusFieldLengthCheck fixData start");
        List resultList = new ArrayList();
        Map resultMap = new HashMap<String,String>();
        String result = resultOk;
        String divid = (String)dataMap.get("divid");
        
        String dbType = recordSet.getDBType();
        String updateSql = "";
        //判断数据库类型
        if("sqlserver".equals((dbType))) {
            updateSql = "ALTER TABLE workflow_requestbase ALTER COLUMN STATUS VARCHAR(500)";
        }else{
            updateSql = "ALTER TABLE workflow_requestbase MODIFY STATUS VARCHAR2(500)";
        }
        
        //执行修改字段长度sql
        recordSet.executeUpdate(updateSql);
        (new BaseBean()).writeLog("WorkflowStatusFieldLengthCheck fixData Sql:" + updateSql);
        resultMap.put("result",result);
        resultMap.put("fixMessage","修复成功");
        resultMap.put("divid",divid);
        resultList.add(resultMap);
        (new BaseBean()).writeLog("WorkflowStatusFieldLengthCheck fixData end");
        return formatReturnJsonFix(result, resultList,this);
    }

    @Override
    public String getClassDesc() {
        return "检测流程表status字段长度是否小于500";
    }

    @Override
    public String getClassName() {
        return "流程表status字段长度检测";
    }
}
